<?php
/**
 * 菜单
 * */
namespace app\admin\model;

use think\Model;
use think\Db;
use app\admin\validate;

class Menu extends Model
{

   
    //自定义初始化
    protected function initialize()
    {
        parent::initialize();
        $this->db = db('menu');
    }

    /**
     * 查询数据 并且缓存
     * @return array;
     */
    public function menuData(){
        $order = 'sort asc';
        $res = $this->db->cache(config('data_expire'))->order($order)->select();
        //格式化数据
        foreach ($res as $k=>$v){
            $res[$k]['url'] = '99';
        }
        return $res;
    }

    /**
     * 单条数据信息
     * */
    public function menuInfo($id,$field=null){
        $where['id'] = $id;
        $res = $this->db->where($where)->field($field)->find();
        return $res;
    }

    /**
     * 将menuData 转换，把数据id放入到数组键名
     * @return $result = array('id'=>array('id'=>''),'id'=>array('id'=>''));
     * */
    public function dataFormat(){
        $arr = $this->menuData();
        foreach($arr as $k=>$v){
            $result[$v['id']]  = $v;
        }
        return $result;
    }

    /**
     * 菜单查询
     * */
    public  function menuWhere($parent_id=0,$type=1,$is_show=null){
        $where['parent_id'] = $parent_id;
        $where['type'] = $type;
        if($is_show){
            $where['is_show'] = $is_show;
        }
        $order = 'sort asc';
        $res = $this->db->where($where)->cache(config('data_expire'))->order($order)->select();
        foreach($res as $k=>$v){
            $res[$k]['url'] = $this->formatUrl($v['model'],$v['controller'],$v['action'],$v['param']);
        }
        return $res;
    }

    /**
     * 删除输出
     * @param $id int;
     * @return int  1成功，0失败
     * */
    public function menuDelete($id){
        // 检测是否可以删除
        $info = $this->dataFormat();
        if($info[$id]['is_delete'] ==1){
            $res = '';
        }else{
            $res = db('menu')->delete($id);
        }
        return $res;
    }

    /**
     * 添加数据
     * @param array $data 数组
     * */
    public function menuInsert($data){
        $res = db('menu')->insert($data);
        return $res;
    }


    /**
     * 更新数据
     * */
    public function menuUpdate($id,$data){
        $where['id'] = $id;
        $res = $this->db->where($where)->update($data);
        return $res;
    }

    /**
     * 子菜单查询
     * */


    /**
     * 后台菜单
     * */
    public function menuAdmin($parent_id=0,$type=1,$is_show=null){
        // 父级菜单
        $res = $this->menuWhere($parent_id,$type,$is_show);
        // 子菜单
        foreach($res as $k=>$v){
            $res[$k]['child'] = $this->menuWhere($v['id'],1,$is_show);
        }
        return $res;
    }


    /**
     *  转换连接地址
     * @param $m 模型
     * @param $c  控制器
     * @param $a  方法
     * @param $p  参数
     * @return array 合成连接地址
     * */
    public function formatUrl($m=null,$c,$a,$p=null){
        if($m){
            $model = $m.'/';
        }else{
            $model = '';
        }
        $url  = $model.$c.'/'.$a;
        return url($url).$p;
    }







    public function sss(){
        return '234234';
    }

}